package com.example.demo.config;/**
 * Created by kete on 2017-10-30.
 */


import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import redis.clients.jedis.JedisPoolConfig;

import java.lang.reflect.Method;

/**
 * 作者：kete
 * 创建时间：2017-10-30 11:16
 * 功能描述：
 * 版本：
 */
@Configuration
@ConditionalOnProperty(prefix = "guns", name = "redis-open", havingValue = "true")
public class RedisConfig extends CachingConfigurerSupport {

    @Value("${redis.address}")
    private String address;

    @Value("${redis.port}")
    private String port;


    @Bean
    @ConditionalOnProperty(prefix = "guns", name = "redis-open", havingValue = "true")
    public JedisPoolConfig jedisPoolConfig(){
        JedisPoolConfig config = new JedisPoolConfig();
        config.setMaxIdle(300);
        config.setMaxWaitMillis(1000);
        config.setTestOnBorrow(true);
        return config;
    }

    @Bean
    @ConditionalOnProperty(prefix = "guns", name = "redis-open", havingValue = "true")
    public JedisConnectionFactory jedisConnectionFactory(){
        JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory();
        jedisConnectionFactory.setPoolConfig(jedisPoolConfig());
        jedisConnectionFactory.setHostName(address);
        jedisConnectionFactory.setPort(Integer.parseInt(port));
        return jedisConnectionFactory;

    }


    @Override
    @Bean
    @ConditionalOnProperty(prefix = "guns", name = "redis-open", havingValue = "true")
    public KeyGenerator keyGenerator() {
        return new KeyGenerator() {
            @Override
            public Object generate(Object o, Method method, Object... objects) {
                StringBuilder sb = new StringBuilder();
                sb.append(o.getClass().getName());
                sb.append(method.getName());
                for (Object obj : objects) {
                    sb.append(obj.toString());
                }
                return sb.toString();
            }
        };
    }

    @Bean("redisTemplate")
    @ConditionalOnProperty(prefix = "guns", name = "redis-open", havingValue = "true")
    public  RedisTemplate<String,Object> redisTemplate(){
        RedisTemplate<String,Object> redisTemplate = new RedisTemplate();
        redisTemplate.setConnectionFactory(jedisConnectionFactory());

        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
        ObjectMapper om = new ObjectMapper();
        om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        jackson2JsonRedisSerializer.setObjectMapper(om);
        redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
        redisTemplate.afterPropertiesSet();

        return redisTemplate;
    }

    @Bean("stringRedisTemplate")
    @ConditionalOnProperty(prefix = "guns", name = "redis-open", havingValue = "true")
    public StringRedisTemplate stringRedisTemplate(){
        StringRedisTemplate template = new StringRedisTemplate(jedisConnectionFactory());

//        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
//        ObjectMapper om = new ObjectMapper();
//        om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
//        om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
//        jackson2JsonRedisSerializer.setObjectMapper(om);
//        template.setValueSerializer(jackson2JsonRedisSerializer);
//        template.afterPropertiesSet();

        return template;
    }

    @Bean
    @ConditionalOnProperty(prefix = "guns", name = "redis-open", havingValue = "true")
    public CacheManager cacheManager() {
        RedisCacheManager cacheManager= new RedisCacheManager(redisTemplate());
        return cacheManager;
    }
}
